package cn.thr.ymcc.tx_consumer;

import cn.thr.ymcc.constant.Cos;
import cn.thr.ymcc.dto.PayOrderDto;
import cn.thr.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
@Slf4j
@RocketMQMessageListener(
        //消费者名称
        consumerGroup = "TxPayOrderConsumer",
        //主题
        topic = Cos.pay.TOPIC_TX_PAY_ORDER,
        //标签
        selectorExpression = Cos.pay.TAGS_TX_PAY_ORDER )
public class TxPayOrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            //变成字符串
            String jsonStr = new String(body, "utf-8");
            log.info("支付服务中消费者....拉取事务消息..{}",jsonStr);
            //用JOSN工具转成实体类
            PayOrderDto payOrderDto = JSON.parseObject(jsonStr, PayOrderDto.class);

            //保存支付单
            payOrderService.save(payOrderDto);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
